package com.suanfa.linkedlist;

import java.util.Iterator;
import java.util.function.Consumer;

//单向链表
public class SinglyLinkedList implements Iterable<Integer> {
    private Node head = null;//头指针

    /**
     * 加static的标准是看内部类是否用到外部类的属性，如果没有用到则可以加static
     * 推荐能加则加
     * @return
     */


    @Override
    public Iterator<Integer> iterator() {
        //匿名内部类

        return new Iterator<Integer>() {
            Node p = head;
            @Override
            public boolean hasNext() {
                return p!= null;
            }

            @Override
            public Integer next() {
                int v = p.value;
                p= p.next;
                return v;
            }
        };
    }


    /**
     * 节点类
     * 对外暴漏越少越好
     * 内部类实现，对外安全
     */
    private static class Node{
        int value;
        Node next;


        public Node(int value,Node next){
            this.value = value;
            this.next = next;
        }
    }

    public void addFirst(int value){
        //1.链表为空
//        if(head == null){
//            head = new Node(value,null);
//        }else{
//            //头插法
//            head = new Node(value,head);
//        }
        head = new Node(value,head);

        //2.链表非空
    }

    /**
     * 递归遍历
     */
    public void loop3(){
        recursion(head);
    }

    private void recursion(Node curr){
        if(curr==null){
            return;
        }
        System.out.println(curr.value);
        recursion(curr.next);
    }


    public void loop(Consumer<Integer> consumer){
        Node p = head;
        while(p != null){
            consumer.accept(p.value);
            p = p.next;
        }
    }

    public void loop2(Consumer<Integer> consumer){
        for(Node p = head; p != null; p = p.next){
            consumer.accept(p.value);
        }
    }

    private Node findLast(){
        if (head == null) return null;
        Node p;
        for(p= head; p.next!=null;p= p.next){}
        return p;
    }

    public void addLast(int value){
        Node last = findLast();
        if(last == null){
            addFirst(value);
            return;
        }
        last.next = new Node(value,last.next);

    }

    public void test(){
        int i =0;
        for(Node p = head ;p!=null;p=p.next,i++){
            System.out.println(p.value+"索引是"+i);
        }
    }

    public Node findNode(int index){
        int i =0;
        for(Node p = head;p!=null;p=p.next,i++){
            if(i==index){
                return p;
            }
        }
        return null;
    }

    public int get(int index){
        Node node = findNode(index);
        if(node == null) {
            throw new IllegalArgumentException(String.format("Index %d is not found", index));
        }
        return node.value;
    }

    public void insert(int index,int value){
        if(index==0){
            addFirst(value);
            return;
        }
        Node prev = findNode(index-1);
        if(prev == null) {
            throw new IllegalArgumentException(String.format("Index %d is not found", index));
        }
        prev.next = new Node(value,prev.next);
    }

    public void removeFirst(){
        if(head == null) throw new IllegalStateException();
        head = head.next;
    }

    public void remove(int index){
        Node prev =findNode(index-1);
        prev.next = prev.next.next;

    }








    public static void main(String args[]){
        SinglyLinkedList list = new SinglyLinkedList();
        list.addFirst(1);
        list.addFirst(2);
        list.addFirst(3);
        list.addFirst(4);
        list.loop2(value -> System.out.println(value));
    }

}

